M=2000;
N=1000;

%Get MNIST data
[trainImg trainLab testImg testLab] = getMNISTDigits(M,N);

%training digits
X=zeros(M,28*28);
for i=1:M
    curDigit=trainImg(:,:,i);
    X(i,:)=curDigit(:);
end

%testing digits
Y=zeros(N,28*28);
for i=1:N
    curDigit=testImg(:,:,i);
    Y(i,:)=curDigit(:);
end

%matrix for distances
dists=zeros(N,M);

for j=1:M
    train = X(j,:);
    for i=1:N
        test = Y(i,:);
        
        %Compute L2 norm 
        dists(i,j)= norm(test-train);
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part b
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%construct confusion matrix
[cMat img] = drawConfusionMatrix(dists,testLab,trainLab);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part c
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%find matches and imposters
[matches imposters] = matchesAndImposters(dists,testLab,trainLab);

%Normalize to [0,250]
mMax=max(matches);
matches=250*matches/mMax;

%Normalize to [0,250]
iMax=max(imposters);
imposters=250*imposters/iMax;

%compute histograms
mCounts = hist(matches,0:10:250);
iCounts = hist(imposters,0:10:250);

%plot it
fig=figure;
title('Match Distances');
hold on;
bar(0:10:250,[mCounts./sum(mCounts);iCounts./sum(iCounts)]','BarWidth',1);
hold on;
lgnd=legend('Matches','Imposters');
set(lgnd,'Location','NorthWest');
set(gca,'XTick',0:10:250);
set(gca,'XLim',[0 260]);
hold off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part d
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Compute and plot ROC curves
[pts eer]=drawROCCurve(matches,imposters,.001,'ROC Curve');

disp(['Equal Error Rate = ',num2str(eer)]);

